//
//  YTCountryCodeModel.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/2/22.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "YTCountryCodeModel.h"

@implementation YTCountryCodeModel

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"countryCodeId" : @"id",};
}

+ (NSString *)primaryKey {
    return @"countryCodeId";
}

@end

@implementation YTCountryCodeModelCache

+ (void)cacheCountryCode:(NSArray<YTCountryCodeModel *> *)codeArray {
    [[YTRealmManager defaultRealm] transactionWithBlock:^{
        [[YTRealmManager defaultRealm] addOrUpdateObjectsFromArray:codeArray];
    }];
    [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:kCountryCodeCache];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)readObjectCompletion:(void (^)(NSArray<YTCountryCodeModel *> *))completion {
    RLMResults<YTCountryCodeModel *> *models = [YTCountryCodeModel allObjects];
    completion([self resultEnumerator:models]);
}

+ (void)searchObjectsWhere:(NSString *)whereAssert
                completion:(void (^)(NSArray<YTCountryCodeModel *> *))completion {
    RLMResults<YTCountryCodeModel *> *models = [YTCountryCodeModel objectsWhere:whereAssert];
    completion([self resultEnumerator:models]);
}

+ (NSMutableArray<YTCountryCodeModel *> *)resultEnumerator:(RLMResults<YTCountryCodeModel *> *)results {
    NSMutableArray<YTCountryCodeModel *> *array = [NSMutableArray array];
    for (YTCountryCodeModel *model in results) {
        [array addObject:model];
    }
    return array;
}

@end
